package com.winai.launcher.utils;

import android.annotation.SuppressLint;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.winai.launcher.R;
import com.winai.launcher.override.CircleCornerForm;

@SuppressLint("CheckResult")
public class PicassoLoadUtils {
    /**
     * 借助内部类 实现线程安全的单例模式
     * 属于懒汉式单例，因为Java机制规定，内部类SingletonHolder只有在getInstance()
     * 方法第一次调用的时候才会被加载（实现了lazy），而且其加载过程是线程安全的。
     * 内部类加载的时候实例化一次instance。
     */
    public PicassoLoadUtils() {
    }

    private static class GlideLoadUtilsHolder {
        private final static PicassoLoadUtils INSTANCE = new PicassoLoadUtils();
    }

    public static PicassoLoadUtils getInstance() {
        return GlideLoadUtilsHolder.INSTANCE;
    }

    /**
     * Glide 加载 简单判空封装 防止异步加载数据时调用Glide 抛出异常
     *
     * @param url       加载图片的url地址  String
     * @param imageView 加载图片的ImageView 控件
     */
    public void picassoLoad(String url, ImageView imageView) {
        picassoLoadRound(url, imageView, 0);
    }

    public void picassoLoad(int url, ImageView imageView) {
        picassoLoadRound(url, imageView, 0);
    }

    /**
     * @desc 加载圆角图片
     */
    public void picassoLoadRound(String url, ImageView imageView, int roundingRadius) {
        loadWithPicasso(imageView, roundingRadius, Picasso.get()
                .load(url));
    }

    public void picassoLoadRound(int url, ImageView imageView, int roundingRadius) {
        loadWithPicasso(imageView, roundingRadius, Picasso.get()
                .load(url));
    }

    private void loadWithPicasso(ImageView imageView, int roundingRadius, RequestCreator load) {
        load
                .transform(new CircleCornerForm(roundingRadius))
                .placeholder(R.mipmap._image_loading)
                .error(R.mipmap._image_loading)
                .into(imageView);
    }

    /**
     * @desc 无预加载图片
     */
    public void picassoLoadNoPlaceholder(String url, ImageView imageView) {
        Picasso.get()
                .load(url)
                .into(imageView);
    }

    /**
     * @desc 无预加载图片
     */
    public void picassoLoadNoPlaceholder(int url, ImageView imageView) {
        Picasso.get()
                .load(url)
                .into(imageView);
    }

    /**
     * @desc 无预加载圆角图片
     */
    public void picassoLoad(int url, ImageView imageView, int placeholder, int roundingRadius) {
        Picasso.get()
                .load(url)
                .transform(new CircleCornerForm(roundingRadius))
                .placeholder(placeholder)
                .error(placeholder)
                .into(imageView);
    }

    /**
     * @desc 无预加载圆角图片
     */
    public void picassoLoad(String url, ImageView imageView, int placeholder, int roundingRadius) {
        Picasso.get()
                .load(url)
                .transform(new CircleCornerForm(roundingRadius))
                .placeholder(placeholder)
                .error(placeholder)
                .into(imageView);
    }

    /**
     * @desc 无预加载圆角图片
     */
    public void picassoLoadNoPlaceholder(int url, ImageView imageView, int roundingRadius) {
        Picasso.get()
                .load(url)
                .transform(new CircleCornerForm(roundingRadius))
                .into(imageView);
    }

    /**
     * @desc 无预加载圆角图片
     */
    public void picassoLoadNoPlaceholder(String url, ImageView imageView, int roundingRadius) {
        Picasso.get()
                .load(url)
                .transform(new CircleCornerForm(roundingRadius))
                .into(imageView);
    }
}
